package com.ksv.lop;

import com.ksv.qlsv.R;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

public class Alerts {
	public static boolean bXoa = false;
	public static void ShowEmpAddedAlert(Context con)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(con);
		builder.setTitle("Add new Employee");
		builder.setIcon(android.R.drawable.ic_dialog_info);
		DialogListner listner = new DialogListner();
		builder.setMessage("Employee Added successfully");
		builder.setPositiveButton("ok", listner);
		AlertDialog diag = builder.create();
		diag.show();
	}
	
	public static void ShowEmpAddedAlert(Context con, String title, String note)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(con);
		builder.setTitle(title);
		builder.setIcon(android.R.drawable.ic_dialog_info);
		DialogListner listner = new DialogListner();
		builder.setMessage(note);
		builder.setPositiveButton("Ok", listner);
		AlertDialog diag = builder.create();
		diag.show();
	}
	
	public static AlertDialog ShowDeleteDialog(final Context con){
		AlertDialog.Builder b = new AlertDialog.Builder(con);
		b.setMessage("Are you sure you want to exit?");
		
		b.setCancelable(false);
		
		b.setNegativeButton("No", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				bXoa = false;
			}
		});
		
		b.setPositiveButton("Yes", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				bXoa = true;
			}
		});
		return b.create();
	}
	
	public static AlertDialog ShowEditDialog_Khoa(final Context con, final Khoa kh)
	{
		AlertDialog.Builder b = new AlertDialog.Builder(con);
		b.setTitle(con.getResources().getString(R.string.khoa));
		LayoutInflater li = LayoutInflater.from(con);
		View v = li.inflate(R.layout.frmkhoa_editdialog, null);
		
		b.setIcon(android.R.drawable.ic_input_get);
		
		b.setView(v);
		final TextView txt_ten_khoa = (TextView)v.findViewById(R.id.txt_ten_khoa);
		final TextView txt_ma_khoa = (TextView)v.findViewById(R.id.txt_ma_khoa);
		
		txt_ten_khoa.setText(kh.getTenKhoa());
		txt_ma_khoa.setText(String.valueOf(kh.getMaKhoa()));
		
		txt_ma_khoa.setEnabled(false);
		
		b.setNegativeButton(con.getResources().getString(R.string.huy), null);
		
		b.setPositiveButton(con.getResources().getString(R.string.luu), new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				kh.setTenKhoa(txt_ten_khoa.getText().toString());
				try{
					ConnectDB.updateKhoa(kh);
					Alerts.ShowEmpAddedAlert(con, "", "Update success");
				}
				catch(Exception ex)
				{
					CatchError(con, ex.toString());
				}
			}
		});
		
		b.setNeutralButton(con.getResources().getString(R.string.xoa), new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				try{
					ConnectDB.deleteKhoa(kh);
					Alerts.ShowEmpAddedAlert(con, "", "Delete success");
				}
				catch(Exception ex)
				{
					CatchError(con, ex.toString());
				}
			}
		});
		
		return b.create();
		
	}
	
	static public void CatchError(Context con, String Exception)
	{
		Dialog diag = new Dialog(con);
		diag.setTitle("Error");
		TextView txt = new TextView(con);
		txt.setText(Exception);
		diag.setContentView(txt);
		diag.show();
	}
}


